#coding=utf-8
import time

import requests

class WadeProxys():
    def __init__(self):
        print("使用匿名接口")


    def getproxys_Str(self,IP,Port):
        # 返回代理request需要的proxy字段内容
        proxyHost = f"{IP}"
        proxyPort = f"{Port}"

        proxyMeta = "http://%(host)s:%(port)s" % {
            "host": proxyHost,
            "port": proxyPort,
        }
        proxies = {
            "http": proxyMeta,
        }
        return proxies
    def getHeiDongIps(self,url):
        #黑洞api https://www.heidongip.com/Index-getapi.html
        mysession = requests.session()
        time.sleep(1)
        while True:

            mysession.keep_alive = False  # 关闭多余连接
            html = mysession.get(url)
            if html.json()['code'] == 0 and html.json()['success'] == 'true':
                return html.json()['data']

            elif html.json()['code'] == 10503 and html.json()['success'] == "false":
                print(html.json())
                print('调用太快了')






if __name__ == '__main__':
    # 获取代理，填写获取api的url
    proxyObj = WadeProxys()
    proxys = proxyObj.getHeiDongIps(url = 'http://ip.ipjldl.com/index.php/api/entry?method=proxyServer.hdtiqu_api_url&packid=7&fa=1&groupid=0&fetch_key=&time=1&qty=54&port=1&format=json&ss=5&css=&dt=&pro=&city=&usertype=4')
    print(proxys)

    # 请求地址
    targetUrl = "http://www.tbzhu.com"

    # 代理服务器
    proxyHost = f"{proxys[0]['IP']}"
    proxyPort = f"{proxys[0]['Port']}"

    proxyMeta = "http://%(host)s:%(port)s" % {
        "host": proxyHost,
        "port": proxyPort,
    }
    proxies = {
        "http": proxyMeta,
    }
    print(proxies)
    resp = requests.get(targetUrl, proxies=proxies)
    print(resp.status_code)
    print(resp.text)